<!DOCTYPE html>
<html>
<head>
    <title>物流发货</title>
    <include file='public/head'></include>
    <style>
        .layui-legend-size {
            font-size: 14px !important;
        }

        .layui-nopadding {
            padding: 0 !important;
        }

        .layui-overflow {
            overflow: auto;
        }

        .layui-nomargin {
            margin: 0 !important;
        }

        .layui-bottom {
            border-bottom: 1px solid rgb(226, 226, 226);
        }

        .layui-input {
            height: 30px !important;
        }

        .layui-form-search-item .layui-inline {
            margin-bottom: 5px;
        }

        .layui-form-search-item label {
            margin-left: 15px;
        }

        .layui-table-page {
            padding: 7px 0px 0px;
        }

        .layui-field-padding {
            border-width: 1px;
            padding: 0 0 5px 10px;
        }
    </style>
</head>
<body>
<div class="layui-tab">
    <form class="layui-form" id="f" method="post">
        <!-- <label class="layui-form-label" style="font-size:20px">基本信息</label> -->
        <table class="layui-table layui-nomargin" style="text-align: center;margin-top:20px">
            <thead>
            <tr>
                <td>订单编号</td>
                <td>收货人</td>
                <td>手机号码</td>
                <td>省|自治区|直辖市</td>
                <td>市|自治州|地区|县</td>
                <td>区|县|镇|乡|街道</td>
                <td style="width:200px;">详细地址</td>
                <td>配送方式</td>
                <td>物流单号</td>
            </tr>
            </thead>
            <tbody>
            <volist name="data" id="vo">
                <input type="hidden" name="id[]" value="{$vo.id}">
                <tr>
                    <td>{$vo.order_num}
                        <input type="hidden" name="order_num[]" value="{$vo.order_num}"></td>
                    <td>{$vo.name}
                        <!-- <input type="text" name="$vo.id[name]"  class="layui-input" required="" value="{$vo.name}"> -->
                    </td>
                    <td>{$vo.mobile}
                        <!-- <input type="text" name="mobile"  class="layui-input" required="" value="{$vo.mobile}"> -->
                    </td>
                    <td>{$vo.province}
                        <!-- <input type="text" name="province"  class="layui-input" required="" value="{$vo.province}"> -->
                    </td>
                    <td>{$vo.city}
                        <!-- <input type="text" name="city"  class="layui-input" required="" value="{$vo.city}"> -->
                    </td>
                    <td>{$vo.area}
                        <!-- <input type="text" name="area"  class="layui-input" required="" value="{$vo.area}"> -->
                    </td>
                    <td>{$vo.address}
                        <!-- <input type="text" name="address"  class="layui-input" required="" value="{$vo.address}"> -->
                    </td>
                    <td>
                        <select type="text" name="code[]" autocomplete="off" placeholder="选择物流公司" lay-verify="required"
                                class="layui-select" lay-search lay-filter="code" data-logistics="{$vo.logistics_code}"
                                data-id="{$vo.id}" data-order_num="{$vo.order_num}">
                            <volist name="logistics" id="lo">
                                <option value="{$lo.code}"
                                <if condition="$lo.code eq $company">selected="selected"</if>
                                >{$lo.company}</option>
                            </volist>
                        </select>
                    </td>
                    <td>
                        <input type="text" name="logistics_code[]" required="required" class="layui-input"
                               id="logistics_code{$vo.id}" required=""
                               value="{$vo.logistics_code ? $vo.logistics_code : ''}">
                    </td>
                </tr>

            </volist>

            </tbody>
        </table>
    </form>
    <div style="width: 220px;bottom:0px; right: 0px; border:0px solid ;position:fixed">
        合计订单:{:count($data)}个
    </div>
</div>
</body>
<include file="public/scripts"/>
<script>
    layui.use(['form'], function () {
        //必须要初始化 form
        var form = layui.form;
        form.on('select(code)', function (data) {
            var company = "{$company}";
            var id = $(this).parent().parent().parent().find('select').data('id');
//  var logistics = $(this).parent().parent().parent().find('select').data('logistics');
            var order_num = $(this).parent().parent().parent().find('select').data('order_num');
            //判断当前选中的值是否跟input的值相等，相等的话，就不发生change事件,需要注意的是要先判断再赋值
            var code = data.value;
            let url = "{:cmf_plugin_url('Logistics://logistics_order/logisticsCode')}";
//  if(data.value !== company ){

            $.ajax({
                url: url,
                data: {code: code, order_num: order_num},
                type: 'post',
                success: function (data) {
                    if (data.code == 200) {
                        var val = data.data ? data.data : '';
                        var html = '<input type="text" name="logistics_code[]" class="layui-input" id="logistics_code' + id + '" required="" value="' + val + '">';
                        $('#logistics_code' + id).replaceWith(html);
                        form.render();
                    } else {
                        layer.msg(data.message, {icon: 2, time: 1500});
                    }
                },
                error: function () {
                    layer.msg('系统错误', {icon: 2, time: 1500});
                }
            })
//  }else{
            // var html = '<input type="text" name="logistics_code[]" class="layui-input" id="logistics_code'+id+'" required="" value="'+logistics+'">';
            // var html = '<input type="text" name="logistics_code[]" class="layui-input" id="logistics_code'+id+'" required="" value="">';
            // $('#logistics_code'+id).replaceWith(html);
            // form.render();
//  }

        })
    });

    var tijiao = function () {
        var dara = $("#f").serialize();
        $.ajax({
            url: "{:cmf_plugin_url('logistics://logistics_order/preserveLogistics')}",
            data: dara,
            type: 'post',
            success: function (data) {
                if (data.code == 200) {
                    layer.msg(data.message, {icon: 1, time: 1500}, function () {
                        parent.layer.closeAll();
                        parent.location.reload();
                    });
                } else {
                    layer.msg(data.message, {icon: 2, time: 1500});
                }
            },
            error: function () {
                layer.msg('系统错误', {icon: 2, time: 1500});
            }
        })
    }
</script>
</html>